/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests;

import java.util.ArrayList;


/**
 * This class collects all the search result.
 */
public final class TestResult
{
    /** Initiator. */
    private static String initiator = null;

    /** List of the class tests. */
    private static ArrayList classTests;

    /** List of the class tests in failure. */
    private static ArrayList failureClassTests;

    /** List of the class tests in error. */
    private static ArrayList errorClassTests;

    /** Number of tests. */
    private static int nbTests;

    /** Number of class tests. */
    private static int nbClassTests;

    /** Number of method tests. */
    private static int nbMethodTests;

    /** Current tests class. */
    private static TestClass currentClass;

    /** TestResult instance. */
    private static TestResult _instance = null;

    /**
     * Constructor of the TestResult class.
     */
    private TestResult()
    {
        classTests = new ArrayList();
        failureClassTests = new ArrayList();
        errorClassTests = new ArrayList();

        nbTests = 0;
        nbClassTests = 0;
        nbMethodTests = 0;
    }

    /**
     * Gets the instance of the TestResult class.
     *
     * @return TestResult instance.
     */
    public static TestResult getInstance()
    {
        if (_instance == null)
        {
            _instance = new TestResult();
        }

        return _instance;
    }

    /**
     * Sets the initiator of the tests.
     *
     * @param name  The class name of the initiator.
     */
    public void setInitiator(String name)
    {
        if (initiator == null)
        {
            initiator = name;
        }
    }

    /**
     * Indicates if the class is the initiator of the tests.
     *
     * @param name  The name of the class.
     *
     * @return true if the class is the initiator, false otherwise.
     */
    public boolean isInitiator(String name)
    {
        return initiator.equals(name);
    }

    /**
     * Indicates the beginning of a class test.
     *
     * @param name  The name of the class.
     */
    public void beginTestClass(String name)
    {
        currentClass = new TestClass(name);
    }

    /**
     * Indicates the end of a class test.
     */
    public void endTestClass()
    {
        if (currentClass.hasClassErrors())
        {
            errorClassTests.add(currentClass);
        }
        else if (currentClass.hasClassFailures())
        {
            failureClassTests.add(currentClass);
        }
        else
        {
            classTests.add(currentClass);
        }

        nbClassTests++;
    }

    /**
     * Indicates the beginning of a method test.
     *
     * @param method  The name of the method.
     */
    public void beginTestMethod(String method)
    {
        currentClass.beginTestMethod(method);
    }

    /**
     * Indicates the end of a method test.
     */
    public void endTestMethod()
    {
        currentClass.endTestMethod();
        nbMethodTests++;
    }

    /**
     * Adds an error.
     *
     * @param error  The error message.
     */
    public void addErrors(String error)
    {
        nbTests++;
        currentClass.addErrors(error);
    }

    /**
     * Adds a failure.
     *
     * @param fail  The failure message.
     */
    public void addFailure(String fail)
    {
        nbTests++;
        currentClass.addFails(fail);
    }

    /**
     * Adds a success.
     *
     * @param success  The success message.
     */
    public void addSuccess(String success)
    {
        nbTests++;
        currentClass.addSuccess(success);
    }

    /**
     * Gets the number of tests.
     *
     * @return The tests number.
     */
    public int getTestsNumber()
    {
        return nbTests;
    }

    /**
     * Gets the number of class tests.
     *
     * @return The tests number.
     */
    public int getClassTestsNumber()
    {
        return nbClassTests;
    }

    /**
     * Gets the number of method tests.
     *
     * @return The tests number.
     */
    public int getMethodTestsNumber()
    {
        return nbMethodTests;
    }

    /**
     * Gets the number of successful class tests .
     *
     * @return The tests number.
     */
    public int getSuccessClassTestsNumber()
    {
        return classTests.size();
    }

    /**
     * Gets the number of class tests in failure.
     *
     * @return The tests number.
     */
    public int getFailureClassTestsNumber()
    {
        return failureClassTests.size();
    }

    /**
     * Gets the number of class tests in error.
     *
     * @return The tests number.
     */
    public int getErrorClassTestsNumber()
    {
        return errorClassTests.size();
    }

    /**
     * Gets the successful test classes.
     *
     * @return The list of classes.
     */
    public ArrayList getSuccessClassTests()
    {
        return classTests;
    }

    /**
     * Gets the test classes in failure.
     *
     * @return The list of classes.
     */
    public ArrayList getFailureClassTests()
    {
        return failureClassTests;
    }

    /**
     * Gets the test classes in error.
     *
     * @return The list of classes.
     */
    public ArrayList getErrorClassTests()
    {
        return errorClassTests;
    }
}
